use std::{fmt::{self}, path::PathBuf, str::FromStr};
use clap::Parser;
use crate::cli::verify_input_filename;
use super::verify_path;

#[derive(Debug, Parser)]
pub enum HttpSubCommand {
    #[command( about= "Serve a directory over HTTP")]
    Serve(HttpServeOpts),
   

}

#[derive(Debug, Parser)]
pub struct HttpServeOpts {
    // #[arg(short,long,value_parser= super::verify_path, default_value = ".")]
    // pub dir: PathBuf,

    #[arg(short, long, value_parser = verify_path, default_value = ".")]
    pub dir: PathBuf,

    #[arg(short,long, default_value_t = 4000)] 
    pub port: u16,
}

#[derive(Debug, Parser)]
pub struct TextVerifyOpts {
    #[arg(short, long, value_parser = verify_input_filename, default_value = "-")] 
    pub input: String,

    #[arg(short, long)] // "-" 代表 从 标准输入 读取
    pub key: String,

    #[arg(long, value_parser = verify_format, default_value = "blake3")] 
    pub format: TextSignFormat,

    #[arg(short, long)]
    pub sig: String,
}


#[derive(Debug, Clone, Copy)]
pub enum TextSignFormat {
    Blake3,
    Ed25519,
}

fn verify_format(format: &str) -> Result<TextSignFormat, &'static str> {
    match format.to_lowercase().as_str() {
        "blake3" => Ok(TextSignFormat::Blake3),
        "ed25519" => Ok(TextSignFormat::Ed25519),
        _ => Err("Invalid format. Use 'blake3' or 'ed25519'."),

  //  format.parse()
    }

}
    


impl fmt::Display for TextSignFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let format_str = match self {
            &TextSignFormat::Blake3 => "blake3",
            TextSignFormat::Ed25519 => "ed25519",
        };
        write!(f, "{}", format_str)
    }
    // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    //     write!(f,"{}",Into::<&'static str>::into(*self))
    // }
}



impl FromStr for TextSignFormat {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        verify_format(s)
    }
}

impl From<TextSignFormat> for &'static str {
    fn from(format: TextSignFormat) -> Self {
        match format {
            TextSignFormat::Blake3 => "blake3",
            TextSignFormat::Ed25519 => "ed25519",
        }
    }
}

// impl fmt::Display for TextSignFormat {
//     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//         let format_str = match self {
//             TextSignFormat::Blake3 => "blake3",
//             TextSignFormat::Ed25519 => "ed25519",
//         };
//         write!(f, "{}", format_str)
//     }
// }

